/**
 * Copyright 2011 T. Spilman
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.jreg;

import org.apache.commons.exec.ExecuteException;

import com.jreg.Operation.AddOperation;
import com.jreg.Operation.DeleteOperation;
import com.jreg.Operation.QueryOperation;

public class RegistryValue {

	private RegistryKey key;
	private String name;
	private Type type;
	private Object value;

	public RegistryValue(RegistryKey key, String name) {
		setKey(key);
		setName(name);
	}

	public RegistryValue(RegistryKey key, String name, Type type, Object value) {
		this(key, name);
		setValue(type, value);
	}

	public RegistryKey getKey() {
		return key;
	}

	public void setKey(RegistryKey key) {
		if (key == null)
			throw new IllegalArgumentException("key must not be null");
		this.key = key;
	}

	public String getName() {
		return name;
	}

	private void setName(String name) {
		if (name == null)
			throw new IllegalArgumentException("name must not be null");
		this.name = name;
	}

	/**
	 * 
	 * @return
	 * @throws IllegalStateException
	 */
	public Type getType() {
		validate();
		return type == null ? Type.getDefault() : type;
	}

	/**
	 * 
	 * @return
	 * @throws IllegalStateException
	 */
	public Object getValue() {
		validate();
		return value;
	}

	public void setValue(Object value) {
		Type type = Type.getDefault();
		setValue(type, value);
	}

	/**
	 * 
	 * @param type
	 * @param value
	 * @throws IllegalArgumentException
	 *             if <code>type</code> or <code>value</code> is
	 *             <code>null</code>
	 */
	public void setValue(Type type, Object value) {
		if (type == null)
			throw new IllegalArgumentException("type must not be null");
		else if (value == null)
			throw new IllegalArgumentException("value must not be null");
		type.validate(value);
		this.type = type;
		this.value = value;
	}

	private void validate() throws IllegalStateException {
		if (type == null || value == null)
			throw new IllegalStateException("must call query first");
	}

	/**
	 * Tests to see if this value exists in the registry.
	 * 
	 * @return <code>true</code> if the value exists in the registry;
	 *         <code>false</code> otherwise
	 * @throws RegistryException
	 */
	public boolean exists() throws RegistryException {
		String keyName = key.getName();
		QueryOperation op = new QueryOperation(keyName);
		op.setValue(QueryOperation.Value.V);
		String valueName = getName();
		op.setValueName(valueName);
		long timeout = key.getTimeout();
		try {
			op.execute(timeout);
			return true;
		} catch (ExecuteException e) {
			return false;
		}
	}

	/**
	 * Updates this registry value.
	 * 
	 * @throws RegistryException
	 */
	public void query() throws RegistryException {
		RegistryKey key = getKey();
		String keyName = key.getName();
		QueryOperation op = new QueryOperation(keyName);
		op.setValue(QueryOperation.Value.V);
		String valueName = getName();
		op.setValueName(valueName);
		doExecute(op);
	}

	/**
	 * Adds or overwrites this registry value.
	 * 
	 * @throws RegistryException
	 */
	public void add() throws RegistryException {
		if (type == null || value == null)
			throw new IllegalStateException("must set type and value");

		RegistryKey key = getKey();
		String keyName = key.getName();
		AddOperation op = new AddOperation(keyName);
		op.setValue(AddOperation.Value.V);
		String valueName = getName();
		op.setValueName(valueName);
		Type type = getType();
		Object value = getValue();
		op.setData(type, value);
		doExecute(op);
	}

	/**
	 * Deletes this registry value.
	 * 
	 * @throws RegistryException
	 */
	public void delete() throws RegistryException {
		RegistryKey key = getKey();
		String keyName = key.getName();
		DeleteOperation op = new DeleteOperation(keyName);
		op.setValue(DeleteOperation.Value.V);
		String valueName = getName();
		op.setValueName(valueName);
		doExecute(op);
	}

	private void doExecute(Operation op) throws RegistryException {
		long timeout = getKey().getTimeout();
		try {
			op.execute(timeout);
		} catch (ExecuteException e) {
			throw new RegistryException(e);
		}
	}

	public String toString() {
		String name = getName();
		String type = getType().name();
		Object value = getValue();
		return "\t" + name + "\t" + type + "\t" + value;
	}

}